package com.wudi.estate.controller.parkingmanagement;

import com.wudi.estate.bean.parkingmanagement.ParkingUsage;
import com.wudi.estate.service.parkingmanagement.ParkingUsageService;
import com.wudi.estate.util.FileUtils;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SessionAttributes(value = "maps")
@Controller
@RequestMapping("/parkingUsage")
public class ParkingUsageController {
    @Autowired
    private ParkingUsageService parkingUsageService;
//显示列表
    @RequestMapping("/listUi")
    public String listUi(ModelMap map){
        try {
            Map<String,Object> maps = parkingUsageService.selectParkingAndPersonnel();
            map.addAttribute("maps",maps);
            List<Map<String,Object>> lists = parkingUsageService.selectAll();
            map.addAttribute("lists",lists);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parkingUsage_list";
    }
    @RequestMapping("/addUi")
    public String addUi(ModelMap map){
        return "parkingUsage_add";
    }
    @RequestMapping("/add")
    public String add(ParkingUsage parkingUsage, MultipartFile image){
        String file ="";
        try {
            file = FileUtils.fileUpload(image);
            parkingUsage.setImg(file);
            System.out.println(parkingUsage);
            parkingUsageService.insert(parkingUsage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parkingUsage/listUi";
    }
    @RequestMapping("/editUi")
    public String editUi(ModelMap map,long id){
        try {
         ParkingUsage  parkingUsage = parkingUsageService.selectParkingUsage(id);
         map.addAttribute("parkingUsage",parkingUsage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parkingUsage_edit";
    }
    @RequestMapping("/edit")
    public String edit(ParkingUsage parkingUsage,MultipartFile image){
        String fileName = "";
            try {
                if(!image.isEmpty()) {
                    System.out.println("不为空");
                    fileName = FileUtils.fileUpload(image);
                    parkingUsage.setImg(fileName);
                }
                else {
                    System.out.println("为空");
                    ParkingUsage parkingUsage1 = parkingUsageService.selectById(parkingUsage.getId());
                    parkingUsage.setImg(parkingUsage1.getImg());
                }
                System.out.println(parkingUsage);
                parkingUsageService.update(parkingUsage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        return "redirect:/parkingUsage/listUi";
    }
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            parkingUsageService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parkingUsage/listUi";
    }
}
